#include ".\mygameradiobutton.h"

CMyGameRadioButton::CMyGameRadioButton(void)
{
	m_rbsState = RBS_UnSelected;
	m_strText = "A RadioButton";
}

CMyGameRadioButton::~CMyGameRadioButton(void)
{

}

void CMyGameRadioButton::LoadRes(LPCSTR szFileName)
{
	m_sprButtonBmp.Load(NULL,szFileName);
}

void CMyGameRadioButton::SetButtonEnable(BOOL bEnable)
{
	if (bEnable)
	{
		m_rbsState = RBS_UnSelected;
	}
	else
	{
		m_rbsState = RBS_Disable;
	}
}

void CMyGameRadioButton::Update()
{
	if (m_rbsState == RBS_Disable)
	{
		return;
	}	
	if (GetParent() != NULL && GetParent()->GetRadioButtonWnd() != this && m_rbsState == RBS_Selected)
	{
		m_rbsState = RBS_UnSelected;
	}
	if (m_pChildWndLock == NULL && m_rbsState != RBS_Selected)
	{
		m_rbsState = RBS_UnSelected;
	}
	return;
}

void CMyGameRadioButton::Draw(HDC hDC)
{
	CMyGameWindow::Draw(hDC);
	int nSrcX = m_rbsState * m_sprButtonBmp.GetWidth() / 4;
	int nSrcY = 0;
	int nSrcWidth = m_sprButtonBmp.GetWidth() / 4;
	int nSrcHeight = m_sprButtonBmp.GetHeight();
	m_sprButtonBmp.DrawRect(hDC,m_ptScreenPos.x,m_ptScreenPos.y,nSrcX,nSrcY,nSrcWidth,nSrcHeight,TRUE);
	COLORREF crOldColor = GetTextColor(hDC);
	SetTextColor(hDC,m_crTextColor);
	TextOut(hDC,m_ptScreenPos.x + nSrcWidth + 2,m_ptScreenPos.y + 2,m_strText.c_str(),m_strText.length());
	SetTextColor(hDC,crOldColor);
	return;
}

BOOL CMyGameRadioButton::LBtnDown(int x,int y)
{
	if (m_rbsState == RBS_Disable)
	{
		return TRUE;
	}
	m_pChildWndLock = this;
	if (m_rbsState == RBS_UnSelected)
	{
		m_rbsState = RBS_MouseDown;
	}
	return TRUE;
}

BOOL CMyGameRadioButton::LBtnUp(int x,int y)
{
	if (m_pChildWndLock == this)
	{
		if (m_rbsState == RBS_MouseDown)
		{
			m_rbsState = RBS_Selected;
			if (GetParent() != NULL)
			{
				GetParent()->SetRadioButtonWnd(this);
				GetParent()->OnChildMsg(M_Radio_Change,m_strName,NULL);
			}	
		}
	}
	return TRUE;
}

BOOL CMyGameRadioButton::MouseMove(int x,int y)
{
	return TRUE;
}




